clear all
set more off


* Merge Marriages and Separations with Fertility 1997-1998

use "$data\N_1997-1998.dta" , clear

rename y_marriage ym
rename w_educ w_education
rename h_educ h_education
drop if w_nationality>=1 & w_nationality<200
drop if h_nationality>=1 & h_nationality<200
preserve
keep if ym==1000 // figli naturali riconosciuti
save "$data\N_1997-1998_naturali.dta" , replace
restore
drop if ym==1000

* Drop duplicates on merge variables
duplicates tag  ym m_marriage w_born_m w_born_y w_nationality w_res_prov h_born_m h_born_y h_nationality region , gen(rep)
tab rep
duplicates drop  ym m_marriage w_born_m w_born_y w_nationality w_res_prov h_born_m h_born_y h_nationality region , force 	  
count
save "$data\N_1997-1998_merge.dta" , replace


set more off
use "$data\MS_final_dataset.dta" , clear

replace h_nationality= h_citizenship if ym>=2005
replace w_nationality= w_citizenship if ym>=2005
drop _merge replication
sum m_marriage ym w_born_m w_born_y w_nationality h_born_m h_born_y h_nationality region 
replace w_resid_prov =888 if w_resid_prov>200
recode w_resid_prov (888=0) 
rename w_resid_prov w_res_prov

preserve
keep if ym>1998
save "$data\MS_dataset_p2.dta" , replace
restore

* Drop duplicates on merge variables
keep if ym<=1998
duplicates tag  ym m_marriage w_born_m w_born_y w_nationality w_res_prov h_born_m h_born_y h_nationality region , gen(rep)
tab rep
duplicates drop  ym m_marriage w_born_m w_born_y w_nationality w_res_prov h_born_m h_born_y h_nationality region , force 	  
count


* 1. merge: main variables
merge 1:1 ym m_marriage w_born_m w_born_y w_nationality w_res_prov h_born_m h_born_y h_nationality region using "$data\N_1997-1998_merge.dta"
tab _merge 

preserve
keep if _merge==3
drop _merge
save "$data\MSN_merged1.dta" , replace
restore
  
preserve
keep if _merge==2
drop _merge
keep  ym m_marriage w_born_m w_born_y w_nationality w_res_prov h_born_m h_born_y h_nationality region fertility twin c1_birth
duplicates drop  ym m_marriage w_born_m w_born_y w_nationality h_born_m h_born_y h_nationality region , force 	  
save "$data\N_nomerge1.dta" , replace
restore

preserve
keep if _merge==1
drop _merge
drop fertility twin c1_birth
duplicates drop  ym m_marriage w_born_m w_born_y w_nationality h_born_m h_born_y h_nationality region , force 	  
save "$data\MS_nomerge1.dta" , replace
restore 


* 2. merge: relax province 
clear
use "$data\MS_nomerge1.dta" , clear
merge 1:1 ym m_marriage w_born_m w_born_y w_nationality h_born_m h_born_y h_nationality region using "$data\N_nomerge1.dta"
tab _merge

preserve
keep if _merge==3
drop _merge
save "$data\MSN_merged2.dta" , replace
restore
  
preserve
keep if _merge==2
drop _merge
keep  ym m_marriage w_born_m w_born_y w_nationality w_res_prov h_born_m h_born_y h_nationality region fertility twin c1_birth

* Change w_born_y
duplicates drop  ym m_marriage w_born_m  w_nationality h_born_m h_born_y h_nationality w_res_prov region , force 	  
save "$data\N_nomerge2.dta" , replace
restore

preserve
keep if _merge==1
drop _merge
drop fertility twin c1_birth
* Change w_born_y
duplicates drop  ym m_marriage w_born_m  w_nationality h_born_m h_born_y h_nationality w_res_prov region , force 	  
save "$data\MS_nomerge2.dta" , replace
restore 


* 3. merge: Change w_born_y
clear
use "$data\MS_nomerge2.dta" , clear
merge 1:1 ym m_marriage w_born_m w_nationality h_born_m h_born_y h_nationality w_res_prov region using "$data\N_nomerge2.dta"
tab _merge

preserve
keep if _merge==3
drop _merge
save "$data\MSN_merged3.dta" , replace
restore
  
preserve
keep if _merge==2
drop _merge
keep  ym m_marriage w_born_m w_born_y w_nationality w_res_prov h_born_m h_born_y h_nationality region fertility twin c1_birth
* Change h_born_y
duplicates drop  ym m_marriage w_born_m w_born_y w_nationality h_born_m  h_nationality w_res_prov region , force 	  
save "$data\N_nomerge3.dta" , replace
restore

preserve
keep if _merge==1
drop _merge
drop fertility twin c1_birth
* Change h_born_y
duplicates drop  ym m_marriage w_born_m w_born_y w_nationality h_born_m h_nationality w_res_prov region , force 	  
save "$data\MS_nomerge3.dta" , replace
restore 


* 4. merge: Change h_born_y
clear
use "$data\MS_nomerge3.dta" , clear
merge 1:1 ym m_marriage w_born_m w_born_y w_nationality h_born_m h_nationality w_res_prov region using "$data\N_nomerge3.dta"
tab _merge

preserve
keep if _merge==3
drop _merge
save "$data\MSN_merged4.dta" , replace
restore
  
preserve
keep if _merge==2
drop _merge
keep  ym m_marriage w_born_m w_born_y w_nationality w_res_prov h_born_m h_born_y h_nationality region fertility twin c1_birth
* Change w_born_m
duplicates drop  ym m_marriage w_born_y w_nationality h_born_m h_born_y h_nationality w_res_prov region , force 	  
save "$data\N_nomerge4.dta" , replace
restore

preserve
keep if _merge==1
drop _merge
drop fertility twin c1_birth
* Change w_born_m
duplicates drop  ym m_marriage w_born_y w_nationality h_born_m h_born_y h_nationality w_res_prov region , force 	  
save "$data\MS_nomerge4.dta" , replace
restore 


* 5. merge: Change w_born_m
clear
use "$data\MS_nomerge4.dta" , clear
count 
merge 1:1  ym m_marriage w_born_y w_nationality h_born_m h_born_y h_nationality w_res_prov region using "$data\N_nomerge4.dta"
tab _merge

preserve
keep if _merge==3
count
drop _merge
save "$data\MSN_merged5.dta" , replace
restore
  
preserve
keep if _merge==2
drop _merge
keep  ym m_marriage w_born_m w_born_y w_nationality w_res_prov h_born_m h_born_y h_nationality region fertility twin c1_birth
* Change h_born_m
duplicates drop  ym m_marriage w_born_m w_born_y w_nationality h_born_y h_nationality w_res_prov region , force 	  
save "$data\N_nomerge5.dta" , replace
restore

preserve
keep if _merge==1
count
drop _merge
drop fertility twin c1_birth
* Change h_born_m
duplicates drop  ym m_marriage w_born_m w_born_y w_nationality h_born_y h_nationality w_res_prov region , force 	  
save "$data\MS_nomerge5.dta" , replace
restore 


* 6. merge: Change h_born_m
clear
use "$data\MS_nomerge5.dta" , clear
merge 1:1  ym m_marriage w_born_m w_born_y w_nationality h_born_y h_nationality w_res_prov region using "$data\N_nomerge5.dta"
tab _merge

preserve
keep if _merge==2
drop _merge
save "$data\N_momerge_final_p1.dta" , replace
restore 

drop if _merge==2
count
  
append using "$data\MSN_merged1.dta"
append using "$data\MSN_merged2.dta"
append using "$data\MSN_merged3.dta"
append using "$data\MSN_merged4.dta"
append using "$data\MSN_merged5.dta"

count 

append using  "$data\MS_dataset_p2.dta"
  
count

drop _merge rep
foreach var in  fertility twin c1_birth {
rename `var' `var'p1
}
save "$data\MSN_dataset_v1.dta" , replace



* Merge Marriages and Separations with Fertility 1999-2012
 
use "$data\N_1999-2012.dta" , clear
drop if w_born_d==0
recode w_nationality (888=999)
recode h_nationality (888=999)
tab w_nationality
sum w_born_d w_born_m w_born_y w_nationality h_born_d h_born_m h_born_y h_nationality region 
gen hn= h_nationality 
gen wn= w_nationality 

* Drop duplicates on merge variables
duplicates drop  w_born_d w_born_m w_born_y w_nationality h_born_d h_born_m h_born_y h_nationality region , force 	  
save "$data\N_1999-2012_merge.dta" , replace


use "$data\MSN_dataset_v1.dta" , clear

rename h_born_day h_born_d
rename w_born_day w_born_d
sum w_born_d w_born_m w_born_y w_nationality h_born_d h_born_m h_born_y h_nationality region 

* Drop duplicates on merge variables
duplicates drop  w_born_d w_born_m w_born_y w_nationality h_born_d h_born_m h_born_y h_nationality region , force 	

* 1. merge: main variables
merge 1:1 w_born_d w_born_m w_born_y w_nationality h_born_d h_born_m h_born_y h_nationality region using  "$data\N_1999-2012_merge.dta"
tab _merge

preserve
keep if _merge==3
drop _merge
save "$data\MSN_merged1_p2.dta" , replace
restore
  
preserve
keep if _merge==2
drop _merge
keep  w_born_d w_born_m w_born_y w_nationality h_born_d h_born_m h_born_y h_nationality region fertility twin c1_birth
* Change region 
duplicates drop  w_born_d w_born_m w_born_y w_nationality h_born_d h_born_m h_born_y h_nationality  , force 	  
save "$data\N_nomerge1_p2.dta" , replace
restore

preserve
keep if _merge==1
drop _merge
drop fertility twin c1_birth
* Change region 
duplicates drop  w_born_d w_born_m w_born_y w_nationality h_born_d h_born_m h_born_y h_nationality  , force 	  
save "$data\MS_nomerge1_p2.dta" , replace
restore 


* 2. merge: change region 
clear
use "$data\MS_nomerge1_p2.dta" , clear
merge 1:1 w_born_d w_born_m w_born_y w_nationality h_born_d h_born_m h_born_y h_nationality  using "$data\N_nomerge1_p2.dta"
tab _merge

preserve
keep if _merge==3
drop _merge
save "$data\MSN_merged2_p2.dta" , replace
restore
  
preserve
keep if _merge==2
drop _merge
keep  w_born_d w_born_m w_born_y w_nationality h_born_d h_born_m h_born_y h_nationality region fertility twin c1_birth
* Change w_born_d
duplicates drop w_born_m w_born_y w_nationality h_born_d h_born_m h_born_y h_nationality region  , force 	  
save "$data\N_nomerge2_p2.dta" , replace
restore

preserve
keep if _merge==1
drop _merge
drop fertility twin c1_birth
* Change w_born_d
duplicates drop   w_born_m w_born_y w_nationality h_born_d h_born_m h_born_y h_nationality region , force 	  
save "$data\MS_nomerge2_p2.dta" , replace
restore 


* 3. merge: change w_born_d 
clear
use "$data\MS_nomerge2_p2.dta" , clear
count 
merge 1:1  w_born_m w_born_y w_nationality h_born_d h_born_m h_born_y h_nationality region using "$data\N_nomerge2_p2.dta"
tab _merge

preserve
keep if _merge==3
drop _merge
save "$data\MSN_merged3_p2.dta" , replace
restore
  
preserve
keep if _merge==2
drop _merge
keep  w_born_d w_born_m w_born_y w_nationality h_born_d h_born_m h_born_y h_nationality region fertility twin c1_birth
* Change h_born_d
duplicates drop  w_born_d w_born_m w_born_y w_nationality  h_born_m h_born_y h_nationality region  , force 	  
save "$data\N_nomerge3_p2.dta" , replace
restore

preserve
keep if _merge==1
drop _merge
drop fertility twin c1_birth
* Change h_born_d
duplicates drop  w_born_d w_born_m w_born_y w_nationality  h_born_m h_born_y h_nationality region , force 	  
save "$data\MS_nomerge3_p2.dta" , replace
restore 


* 4. merge: change h_born_d 
clear
use "$data\MS_nomerge3_p2.dta" , clear
merge 1:1   w_born_d w_born_m w_born_y w_nationality  h_born_m h_born_y h_nationality region using "$data\N_nomerge3_p2.dta"
tab _merge

preserve
keep if _merge==3
drop _merge
save "$data\MSN_merged4_p2.dta" , replace
restore
  
preserve
keep if _merge==2
drop _merge
keep    w_born_d w_born_m w_born_y w_nationality h_born_d h_born_m h_born_y h_nationality region fertility twin c1_birth
* Change w_born_m
duplicates drop w_born_d  w_born_y w_nationality h_born_d h_born_m h_born_y h_nationality region  , force 	  
save "$data\N_nomerge4_p2.dta" , replace
restore

preserve
keep if _merge==1
drop _merge
drop fertility twin c1_birth
* Change w_born_m
duplicates drop w_born_d  w_born_y w_nationality h_born_d h_born_m h_born_y h_nationality region , force 	  
save "$data\MS_nomerge4_p2.dta" , replace
restore 


* 5. merge: change w_born_m 
clear
use "$data\MS_nomerge4_p2.dta" , clear
count 
merge 1:1   w_born_d w_born_y w_nationality h_born_d h_born_m h_born_y h_nationality region using "$data\N_nomerge4_p2.dta"
tab _merge

preserve
keep if _merge==3
drop _merge
save "$data\MSN_merged5_p2.dta" , replace
restore
  
preserve
keep if _merge==2
drop _merge
keep    w_born_d w_born_m w_born_y w_nationality h_born_d h_born_m h_born_y h_nationality region fertility twin c1_birth
* Change h_born_m
duplicates drop   w_born_d w_born_m w_born_y w_nationality h_born_d h_born_y h_nationality region  , force 	  
save "$data\N_nomerge5_p2.dta" , replace
restore

preserve
keep if _merge==1
drop _merge
drop fertility twin c1_birth
* Change h_born_m
duplicates drop w_born_d w_born_m w_born_y w_nationality h_born_d h_born_y h_nationality region , force 	  
save "$data\MS_nomerge5_p2.dta" , replace
restore 


* 6. merge: change h_born_m 
clear
use "$data\MS_nomerge5_p2.dta" , clear
count 
merge 1:1   w_born_d w_born_m w_born_y w_nationality h_born_d h_born_y h_nationality region using "$data\N_nomerge5_p2.dta"
tab _merge

preserve
keep if _merge==2
drop _merge
save "$data\N_momerge_final_p2.dta" , replace
restore 

drop if _merge==2
drop _merge
count
  
append using "$data\MSN_merged1_p2.dta"
append using "$data\MSN_merged2_p2.dta"
append using "$data\MSN_merged3_p2.dta"
append using "$data\MSN_merged4_p2.dta"
append using "$data\MSN_merged5_p2.dta"

count 

foreach var in  fertility twin c1_birth {
rename `var' `var'p2
}

* Fertility variables 

recode fertilityp1 (.=0)
recode fertilityp2 (.=0)
gen fertility = fertilityp1+fertilityp2
order fertilityp1 fertilityp2 fertility
gen cc = fertility>0
tab cc
label var cc "children yes or no"
gen nc = fertility
replace nc=. if fertility==0
tab fertility
tab nc
codebook nc
label var nc "number of children"
count if  c1_birthp1!= ""
count if  c1_birthp2!= ""
gen c1birth = c1_birthp1 
replace c1birth = c1_birthp2 if  c1_birthp2!= "" &  c1_birthp1== ""

* Correction: marriage date

tostring day_marriage m_marriage ym , replace
replace day_marriage="30" if day_marriage=="31" & m_marriage=="4"
replace day_marriage="30" if day_marriage=="31" & m_marriage=="6"
replace day_marriage="30" if day_marriage=="31" & m_marriage=="9"
replace day_marriage="30" if day_marriage=="31" & m_marriage=="11"
replace day_marriage="28" if day_marriage=="29" & m_marriage=="2"

foreach var in day_marriage m_marriage    {
replace `var'="01" if `var'=="1"
replace `var'="02" if `var'=="2"
replace `var'="03" if `var'=="3"
replace `var'="04" if `var'=="4"
replace `var'="05" if `var'=="5"
replace `var'="06" if `var'=="6"
replace `var'="07" if `var'=="7"
replace `var'="08" if `var'=="8"
replace `var'="09" if `var'=="9"
}
cap drop marriagedate mdate 
gen marriagedate= day_marriage+m_marriage+ym
gen mdate = mofd(date(marriagedate, "DMY"))
gen c1date = mofd(date(c1birth, "DMY"))
format mdate c1date %tm

save "$data\MSN_final_dataset.dta" , replace


